package idv.loveloser.servlet;

import idv.loveloser.dao.MdlAssignment;
import idv.loveloser.dao.MdlAssignmentDAO;
import idv.loveloser.dao.MdlChat;
import idv.loveloser.dao.MdlChatDAO;
import idv.loveloser.dao.MdlCourseModules;
import idv.loveloser.dao.MdlCourseModulesDAO;
import idv.loveloser.dao.MdlCourseSections;
import idv.loveloser.dao.MdlCourseSectionsDAO;
import idv.loveloser.dao.MdlFolder;
import idv.loveloser.dao.MdlFolderDAO;
import idv.loveloser.dao.MdlForum;
import idv.loveloser.dao.MdlForumDAO;
import idv.loveloser.dao.MdlModules;
import idv.loveloser.dao.MdlModulesDAO;
import idv.loveloser.dao.MdlUrl;
import idv.loveloser.dao.MdlUrlDAO;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class GetCourseSections extends HttpServlet {
	private MdlCourseSectionsDAO courseSectionsDAO = new MdlCourseSectionsDAO();
	private MdlModulesDAO modulesDAO = new MdlModulesDAO();
	private MdlAssignmentDAO assignmentDAO = new MdlAssignmentDAO();
	private MdlCourseModulesDAO courseModulesDAO = new MdlCourseModulesDAO();
	private MdlFolderDAO folderDAO = new MdlFolderDAO();
	private MdlForumDAO forumDAO = new MdlForumDAO();
	private MdlUrlDAO urlDAO = new MdlUrlDAO();
//	private MdlAssignmentDAO assignmentDAO =new MdlAssignmentDAO();
	/**
	 * Constructor of the object.
	 */
	public GetCourseSections() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String courseId=request.getParameter("courseId");
		List<CourseContent> returnList=new ArrayList<CourseContent>();
		List<MdlCourseSections> sections=courseSectionsDAO.findByCourseSortBySection(courseId);
		List<MdlCourseModules> modules=courseModulesDAO.findByCourse(Long.parseLong(courseId));
		
		for(int index=0;index<sections.size();index++){
			MdlCourseSections section = sections.get(index);
			String sequence = section.getSequence();
			if(sequence!=null){
				StringTokenizer tokens=new StringTokenizer(sequence,",");
				while(tokens.hasMoreTokens()){
					String moduleId=tokens.nextToken();
					for(int index2=0;index2<modules.size();index2++){
						MdlCourseModules selectedCM=modules.get(index2);
						if(selectedCM.getId()==Long.parseLong(moduleId)){
							if(selectedCM.getModule()==1L){
								CourseContent cc=new CourseContent();
								MdlAssignment assignment=assignmentDAO.findById(selectedCM.getInstance());
								cc.setContent(assignment.getIntro());
								cc.setContentType("Assignment");
								cc.setContentTypeId(1L);
								cc.setCourseId(Long.parseLong(courseId));
								cc.setModuleId(selectedCM.getId());
								cc.setSection(section.getSection());
								cc.setSequence(Long.parseLong(moduleId));
								cc.setInstanceId(selectedCM.getInstance());
								returnList.add(cc);
							}if(selectedCM.getModule()==6L){
								CourseContent cc=new CourseContent();
								MdlFolder folder=folderDAO.findById(selectedCM.getInstance());
								cc.setContent(folder.getIntro());
								cc.setContentType("Folder");
								cc.setContentTypeId(6L);
								cc.setCourseId(Long.parseLong(courseId));
								cc.setModuleId(selectedCM.getId());
								cc.setSection(section.getSection());
								cc.setSequence(Long.parseLong(moduleId));
								cc.setInstanceId(selectedCM.getInstance());
								returnList.add(cc);
							}
							if(selectedCM.getModule()==7L){
								CourseContent cc=new CourseContent();
								MdlForum forum=forumDAO.findById(selectedCM.getInstance());
								cc.setContent(forum.getIntro());
								cc.setContentType("Forum");
								cc.setContentTypeId(7L);
								cc.setCourseId(Long.parseLong(courseId));
								cc.setModuleId(selectedCM.getId());
								cc.setSection(section.getSection());
								cc.setSequence(Long.parseLong(moduleId));
								cc.setInstanceId(selectedCM.getInstance());
								returnList.add(cc);
							}
							if(selectedCM.getModule()==18L){
								CourseContent cc=new CourseContent();
								MdlUrl url=urlDAO.findById(selectedCM.getInstance());
								cc.setContent(url.getIntro());
								cc.setContentType("Url");
								cc.setContentTypeId(18L);
								cc.setCourseId(Long.parseLong(courseId));
								cc.setModuleId(selectedCM.getId());
								cc.setSection(section.getSection());
								cc.setSequence(Long.parseLong(moduleId));
								cc.setInstanceId(selectedCM.getInstance());
								returnList.add(cc);
							}
						}
					}
				}
			}
		}
		
		Gson gson=new Gson();
		System.out.println(gson.toJson(returnList));
		out.println(gson.toJson(returnList));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
